<?php

namespace App\Library;

use Exception;
use OSS\OssClient;

/**
 * 文件处理.
 */
class HandleFile
{
    //检查oss文件是否存在
    public static function checkOssFile($filePath)
    {
        try {
            $oss = new OssClient(config('oss.access_id'), config('oss.access_key'), config('oss.endpoint'));
            $msg = $oss->doesObjectExist(config('oss.bucket'), $filePath);
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        return $msg;
    }

    //处理新旧文件转换
    public static function handleFilePath($oldFilePath, $ossFilePath)
    {
        $ossFile = ltrim($ossFilePath, '/');
        $does    = self::checkOssFile($ossFile);
        
        if ($does === true && $ossFilePath != 'oss') {
            $path = config('oss.oss_url') . $ossFilePath;
            /*if (!my_file_exists($path)) {
        $path = NULL;
        }*/
        } else {
            $path = config('oss.local_url_http') . $oldFilePath;
            /*if (!my_file_exists($path)) {
        $path = NULL;
        }*/
        }

        return $path;
    }

    //批量处理数据集图片(可处理数组)
    public static function batchHandleOssFile($data, $fieldName)
    {
        foreach ($data as &$value) {
            if (is_array($fieldName)) {
                foreach ($fieldName as $val) {
                    $ossFieldName = $val . '_oss';
                    $value->$val  = self::handleFilePath($val, $value->$ossFieldName);
                }
            } else {
                $ossFieldName      = $fieldName . '_oss';
                $value->$fieldName = self::handleFilePath($value->$fieldName, $value->$ossFieldName);
            }
        }

        return $data;
    }

    //移动oss文件.
    public static function moveTmpToNormal($oldFile, $newFile)
    {
        try {
            $oss = new OssClient(config('oss.access_id'), config('oss.access_key'), config('oss.endpoint'));
            //判断文件是否在oss存在
            $does = $oss->doesObjectExist(config('oss.bucket'), $oldFile);
            if (!$does) {
                throw new Exception('oss文件不存在！');
            }
            $res = $oss->copyObject(config('oss.bucket'), $oldFile, config('oss.bucket'), $newFile); //转存
            //$result = $oss->deleteObject(config('env.oss_bucket'), $oldFile); //删除原文件
            //if (!$result) {
            //  throw new Exception('文件删除失败！');
            //}

            $msg = true;
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        return $msg;
    }

    //处理服务器本地文件
    public static function handleOldFile($filePath, $filename)
    {
        try {
            $oss = new OssClient(config('oss.access_id'), config('oss.access_key'), config('oss.endpoint'));

            $oldFilePath = config('oss.local_url_http') . $filePath;

            if (my_file_exists($oldFilePath)) {
                $ext  = substr(strrchr($oldFilePath, '.'), 1);
                $file = public_path('temporary.' . $ext);
                if (!file_exists($file)) {
                    fopen($file, 'w');
                }
                $result = file_put_contents($file, file_get_contents($oldFilePath));
                if ($result) {
                    $res = $oss->uploadFile(config('oss.bucket'), $filename, $file);
                    unlink($file);
                    $msg = true;
                }
            } else {
                $msg = null;
            }
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        return $msg;
    }
}
